<template>
  <div class="user-manage">
    <div class="head" style="margin-bottom: 20px">
      <div class="left">
        <el-button type="primary" @click="customerAddFormVisible = true"
          >增加新客户</el-button
        >
      </div>
      <div class="right">
        <el-input
          v-model="searchText"
          placeholder="请输入客户手机号"
        ></el-input>
        <el-button type="primary" @click="queryCustomer">搜索</el-button>
        <el-button type="primary" @click="getAllCustomer">显示全部</el-button>
      </div>
    </div>
    <hr />
    <!-- 增加窗口 -->
    <el-dialog
      title="增加新客户"
      :visible.sync="customerAddFormVisible"
      width="50%"
      :before-close="handleClose"
    >
      <customer-add-form></customer-add-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="customerAddFormVisible = false">取 消</el-button>
      </span>
    </el-dialog>
    <!-- 编辑窗口 -->
    <el-dialog
      title="编辑客户信息"
      :visible.sync="customerUpdateFormVisible"
      width="50%"
      :before-close="handleClose"
    >
      <customer-update-form :inf="customerUpdateData"></customer-update-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="customerUpdateFormVisible = false">取 消</el-button>
      </span>
    </el-dialog>
    <!-- 开通业务窗口 -->
    <el-dialog
      title="开通订单业务"
      :visible.sync="orderAddFormVisible"
      width="50%"
      :before-close="handleClose"
    >
      <order-add-from :inf="orderAddData"></order-add-from>
      <span slot="footer" class="dialog-footer">
        <el-button @click="orderAddFormVisible = false">取 消</el-button>
      </span>
    </el-dialog>
    <div class="table">
      <el-table :data="tableData" style="width: 100%" stripe height="400">
        <el-table-column label="ID" width="80">
          <template slot-scope="scope">
            <span>{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="姓名" width="100">
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span>
          </template> </el-table-column
        ><el-table-column label="手机号" width="150">
          <template slot-scope="scope">
            <span>{{ scope.row.phone }}</span>
          </template> </el-table-column
        ><el-table-column label="业务状态" width="150">
          <template slot-scope="scope">
            <span v-if="scope.row.order.id == 0">未开通</span>
            <span v-if="new Date(scope.row.order.endTime) >= new Date()"
              >{{ scope.row.order.endTime }} 到期</span
            >
            <span
              v-if="
                new Date(scope.row.order.endTime) <= new Date() &&
                scope.row.order.id != 0
              "
              >已到期</span
            >
          </template>
        </el-table-column>
        <el-table-column label="业务类型" width="120">
          <template slot-scope="scope">
            <span v-if="scope.row.order.serviceId == 1">包年宽带</span>
            <span v-if="scope.row.order.serviceId == 2">包月宽带</span>
          </template>
        </el-table-column>
        <el-table-column label="业务ID号" width="120">
          <template slot-scope="scope">
            <span v-if="scope.row.order.id != 0">{{ scope.row.order.id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              @click="handleDel(scope.$index, scope.row)"
              >删除客户</el-button
            >
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              v-if="scope.row.order.id == 0"
              size="mini"
              type="success"
              @click="handleAdd(scope.$index, scope.row)"
              >业务开通</el-button
            >
            <el-button
              size="mini"
              type="warning"
              @click="handleCtn(scope.row)"
              v-if="
                new Date(scope.row.order.endTime) <= new Date() &&
                scope.row.order.id != 0
              "
              >业务续费</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
   
  </div>
</template>


<script>
import CustomerAddForm from "./CustomerAddForm.vue";
import CustomerUpdateForm from "./CustomerUpdateForm.vue";
import OrderAddFrom from "./OrderAddFrom.vue";
export default {
  name: "UserManage",
  components: {
    CustomerAddForm,
    CustomerUpdateForm,
    OrderAddFrom,
  },
  data() {
    return {
      tableData: "", //表格数据
      searchText: "", //搜索框内容
      customerAddFormVisible: false, //打开增加窗口
      customerUpdateFormVisible: false, //打开编辑窗口
      customerUpdateData: "", //传送给编辑用户表单的数据
      orderAddData: "", //传送给增加业务表单的数据
      orderAddFormVisible: false, //打开开通业务窗口
      
    };
  },
  methods: {
    // 获取所有客户
    getAllCustomer() {
      let _this = this;
      this.$axios({
        url: "/customer/getAllCustomer",
        method: "post",
      }).then(function (resp) {
        _this.tableData = resp.data;
      });
    },
   
    // 搜索客户
    queryCustomer() {
      let _this = this;
      this.$axios({
        url: "/customer/queryCustomer",
        method: "post",
        data: { phone: _this.searchText },
      }).then(function (resp) {
        if (resp.data == "") {
          _this.tableData = "";
        } else {
          _this.tableData = [resp.data];
        }
      });
    },

    // 修改客户信息
    handleEdit(index, row) {
      console.log(index);
      console.log(row);
      // alert(row)
      this.customerUpdateData = {
        oldPhone: row.phone,
        customer: JSON.parse(JSON.stringify(row)),
      };
      this.customerUpdateFormVisible = true;
    },

    // 删除客户
    handleDel(index, row) {
      console.log(index);
      let _this = this;
      let flag = confirm("确定删除吗");
      if (flag) {
        this.$axios({
          url: "/customer/deleteCustomer",
          method: "post",
          data: row,
        }).then(function (resp) {
          if (resp.data == 1) {
            alert("删除成功");
          } else {
            alert("删除失败");
          }
          _this.getAllCustomer();
        });
      }
    },

    //开通业务
    handleAdd(index, row) {
      console.log(index);
      this.orderAddData = {
        customerId: row.id,
        salesOrder: {
          userId: this.$store.state.user.id, //设为当前登录的用户
          startTime: new Date(),
          endTime: new Date(),
          serviceId: 1,
        },
      };
      this.orderAddFormVisible = true;
    },

    // 续费业务
    handleCtn(row) {
      row.order.startTime = new Date();
      if (row.order.serviceId == 1) {
        row.order.endTime = new Date(
          row.order.startTime.getTime() + 31536000000
        );
      } else if (row.order.serviceId == 2) {
        row.order.endTime = new Date(
          row.order.startTime.getTime() + 2628000000
        );
      }
      row.order.userId = 4; //userId根据当前用户修改

      this.$axios({
        url: "/order/updateOrder",
        method: "post",
        data: row.order,
      }).then(function (resp) {
        if (resp.data == 1) {
          alert("更改成功");
        } else {
          alert("更改失败");
        }
      });
      this.getAllCustomer();
    },
  },
  created() {
    this.getAllCustomer();
  },
};
</script>


<style lang="less" scoped>
.user-manage {
  .head {
    display: flex;
    justify-content: space-between;
    .right {
      display: flex;
    }
  }
}
</style>

